package com.aeon.ui;

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import com.aeon.jface.tableViewer.ui.TableViewerDialog;
import com.aeon.jface.treeTable.ui.TreeTableViewerDialog;
import com.aeon.thumbnail.ui.ThumbnailDialog;

public class MYapp {
	private Shell mApplicationShell;
	private Display display;

	private Label mstaLabel;
	private Menu mMenuBar;

	public MYapp() {

		configureApplication();
		init();
	}

	private void configureApplication() {
		display = Display.getDefault();
		mApplicationShell = new Shell(display);

//		mApplicationShell.setSize(200, 300);
	}

	private void init() {
		mApplicationShell.setLayout(new FillLayout());

		mstaLabel = new Label(mApplicationShell, SWT.BORDER);
		mstaLabel.setText("Look at me");

		setCoolBar();
		TextComposite textComposite = new TextComposite(mApplicationShell,
				SWT.NONE);

		menuSetup();

		TableComponent component = new TableComponent(mApplicationShell,
				SWT.BORDER);

		TabFolderComposite composite = new TabFolderComposite(
				mApplicationShell, SWT.BORDER);

		popUpmenuSetUp(mstaLabel);

		StackLayoutComposit layoutComposit = new StackLayoutComposit(
				mApplicationShell, SWT.BORDER_SOLID);
	}

	private void popUpmenuSetUp(Control control) {
		Menu popup = new Menu(mApplicationShell, SWT.POP_UP);
		MenuItem item = new MenuItem(popup, SWT.NORMAL);
		item.setText("popup");

		control.setMenu(popup);

	}

	private void setCoolBar() {

		CoolBar bar = new CoolBar(mApplicationShell, SWT.BORDER);
		bar.setSize(50, 20);
		bar.setLocation(0, 0);
		addCoolItem(bar, "CoolBar1");
		// bar.setLocked(false);
		addCoolItem(bar, "CoolBar4");
		addCoolItem(bar, "CoolBar3");
		addCoolItem(bar, "CoolBar2");
		bar.setWrapIndices(new int[] { 1 });

	}

	private void addCoolItem(CoolBar bar, String string) {
		CoolItem item = new CoolItem(bar, SWT.NONE);
		Button button = new Button(bar, SWT.PUSH);
		button.setText(string);
		item.setControl(button);
		item.setText(string);
		item.setSize(20, 20);
	}

	private void menuSetup() {
		mMenuBar = new Menu(mApplicationShell, SWT.BAR);
		mApplicationShell.setMenuBar(mMenuBar);

		MenuItem parent = new MenuItem(mMenuBar, SWT.CASCADE);
		parent.setText("&Dialog");

		Menu m1 = new Menu(mApplicationShell, SWT.DROP_DOWN);
		parent.setMenu(m1);
		buildColorDlgMenu(m1);
		buildDirectoryDlgMenu(m1);
		buildFontDlgMenu(m1);
		buildPrintDlgMenu(m1);
		buildFileDlgMenu(m1);

		MenuItem aJFace = new MenuItem(mMenuBar, SWT.CASCADE);
		aJFace.setText("&JFace");
		Menu jfaMenu = new Menu(mApplicationShell, SWT.DROP_DOWN);
		aJFace.setMenu(jfaMenu);
		buildJFaceTableMenu(jfaMenu);
		buildTreeTableMenu(jfaMenu);
		
		
		
		MenuItem thumItem=new MenuItem(mMenuBar, SWT.CASCADE);
		thumItem.setText("ThumbNail");
		Menu menu=new Menu(mApplicationShell,SWT.DROP_DOWN);
		buildthumbMenu(menu);
		thumItem.setMenu(menu);
		
		
		
	}

	private void buildthumbMenu(Menu menu) {

		MenuItem item=new MenuItem(menu, SWT.NONE);
		item.setText("Thumbnail");
		item.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				ThumbnailDialog dialog=new ThumbnailDialog(mApplicationShell);
				dialog.open();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

	private void buildJFaceTableMenu(Menu jfaMenu) {
		MenuItem tableItem = new MenuItem(jfaMenu, SWT.None);
		tableItem.setText("TableViewer");
		tableItem.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				TableViewerDialog dialog = new TableViewerDialog(mApplicationShell);
				dialog.open();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void buildTreeTableMenu(Menu jfaMenu) {
		MenuItem tableItem = new MenuItem(jfaMenu, SWT.None);
		tableItem.setText("&TreeTableViewer");
		tableItem.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				TreeTableViewerDialog dialog = new TreeTableViewerDialog(mApplicationShell);
				dialog.open();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});

	}


	
	private void buildColorDlgMenu(Menu parent) {
		MenuItem dlgMenu = new MenuItem(parent, SWT.PUSH);
		dlgMenu.setText("&Color Dialog...  \t Ctrl+g");
		dlgMenu.setAccelerator(SWT.CTRL + 'g');

		dlgMenu.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				ColorDialog dialog = Dialogs
						.createColorDailog(mApplicationShell);
				dialog.open();
				mstaLabel.setBackground(new Color(display, dialog.getRGB()));
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void buildDirectoryDlgMenu(Menu parent) {
		MenuItem dlgMenu = new MenuItem(parent, SWT.PUSH);
		dlgMenu.setText("Directory Dialog...");
		dlgMenu.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = Dialogs
						.createDirectoryDialog(mApplicationShell);
				dialog.open();
				mstaLabel.setText(dialog.getFilterPath());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void buildFileDlgMenu(Menu parent) {
		MenuItem dlgMenu = new MenuItem(parent, SWT.PUSH);
		dlgMenu.setText("File Dialog...");
		dlgMenu.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = Dialogs.createFileDialog(mApplicationShell);
				dialog.open();
				mstaLabel.setText(dialog.getFileName());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void buildFontDlgMenu(Menu parent) {
		MenuItem dlgMenu = new MenuItem(parent, SWT.PUSH);
		dlgMenu.setText("Font Dialog...");
		dlgMenu.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				FontDialog dialog = Dialogs.createFontDialog(mApplicationShell);
				dialog.open();
				mstaLabel.setText(Arrays.toString(dialog.getFontList()));
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void buildPrintDlgMenu(Menu parent) {
		MenuItem dlgMenu = new MenuItem(parent, SWT.PUSH);
		dlgMenu.setText("Print Dialog...");
		dlgMenu.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				PrintDialog dialog = Dialogs
						.createPrintDialog(mApplicationShell);
				dialog.open();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void open() {
//		mApplicationShell.pack();
		mApplicationShell.open();
		while (!mApplicationShell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}

}
